<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="Title" v-model="listQuery.filterItem">
      </el-input>
      <el-button class="filter-item" type="primary"  icon="el-icon-search" @click="handleFilter">search</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">add</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='ID' width="95">
        <template slot-scope="scope">
          {{ 1 + scope.$index + ((listQuery.page -1 ) * listQuery.limit) }}
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template slot-scope="scope">
          {{scope.row.title}}
        </template>
      </el-table-column>
      <el-table-column label="Applicant" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.applicant_name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="Type" width="200" align="center">
        <template slot-scope="scope">
          {{scope.row.type}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Status" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Reason" width="110">
        <template slot-scope="scope">
          <span>{{scope.row.reason}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="Actions" label="Display_time" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleShowDetail(scope.$index)">detail
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogTableVisible">
      <el-form :rules="rules" ref="dataForm" :model="tempApplicationHeader" label-width="95px">
      <el-form-item label="Title" prop='title'>
        <el-input disabled v-model="tempApplicationHeader.title"></el-input>
      </el-form-item>
      <el-form-item label="Date">
        <el-col :span="11">
          <el-date-picker disabled type="date" placeholder="Pick a date" v-model="tempApplicationHeader.create_date" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="Reason">
        <el-input disabled type="textarea" v-model="tempApplicationHeader.reason"></el-input>
      </el-form-item>
      </el-form>
    <el-table :data="tempResourceList" v-loading.body="templistLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='Index' width="95">
        <template slot-scope="scope">
          {{ 1 + scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Resource Name">
        <template slot-scope="scope">
          {{scope.row.resources_name}}
        </template>
      </el-table-column>
      <el-table-column label="Amount" width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.quantity}}
        </template>
      </el-table-column>
      <el-table-column label="Unit" width="80" align="center">
        <template slot-scope="scope">
          {{scope.row.unit}}
        </template>
      </el-table-column>
    </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogTableVisible = false">Close</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMyApplicationList, approve, getApplicationLineList} from '@/api/application'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      templistLoading: false,
      total: null,
      listQuery: {
        page: 1,
        limit: 10,
        filterItem: null,
        type: null,
        status: null,
        sort: null
      },
      tempApplicationHeader: {
        title: '',
        reason: '',
        applicant_id: null,
        applicant_name: '',
        organization_id: null,
        organization_name: '',
        create_date: null
      },
      tempResourceList: [],
      dialogTableVisible: false,
      dialogStatus: '',
      textMap: {
        detail: 'Application Detail'
      },
      rules: {
      },
      importanceOptions: [1, 2, 3],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }]
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getMyApplicationList(this.listQuery).then(response => {
        this.list = response.dataList
        this.total = response.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleFilter() {
      this.getList()
    },
    handleShowDetail(index) {
      this.dialogStatus = 'detail'
      this.templistLoading = true
      getApplicationLineList(this.list[index].header_id).then(response => {
        this.tempResourceList = response.dataList
        this.templistLoading = false
      })
      this.tempApplicationHeader = Object.assign({}, this.list[index])
      this.dialogStatus = 'detail'
      this.dialogTableVisible = true
    },
    handleCreate(){
      this.$router.push({
        name: 'TempResourcesList',
        params: {
        }
      })
    }
  }
}
</script>
